package com.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.entity.Mechanism;
import com.project.entity.Staff;
import com.project.service.MechanismService;
import com.project.util.PageBean;
import com.project.util.R;
import com.project.util.TreeBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Field;
import java.util.*;

/**
 * 单位机构控制器
 *
 * @author Leo高洋
 * @create 2023-02-02 3:14
 */
@RestController
@RequestMapping("/mechanism")
public class MechanismController {
    @Autowired
    private MechanismService mechanismService;

    private final static Logger logger = LoggerFactory.getLogger(MechanismController.class);

    /**
     * 获取全部机构信息
     *
     * @return
     */
    @GetMapping("/list")
    public R mechanismList() {
        logger.info("获取全部机构信息");
        List<Mechanism> mechanismList = mechanismService.getMechanismList();
        Map<String, Object> map = new HashMap<>();
        map.put("mechanismList", mechanismList);
        return R.ok(map);
    }

    /**
     * 根据条件分页查询员工信息
     *
     * @param pageBean
     * @return
     */
    @PostMapping("/listByPage")
    public R staffList(@RequestBody PageBean pageBean) {
        logger.info("根据条件分页查询机构信息");
        Map<String, Object> map = new HashMap<>();
        map.put("name", pageBean.getQuery().trim());
        map.put("start", pageBean.getStart());
        map.put("pageSize", pageBean.getPageSize());
        List<Mechanism> mechanismList = mechanismService.list(map);
        long total = mechanismService.getTotal(map);
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("mechanismList", mechanismList);
        resultMap.put("total", total);
        return R.ok(resultMap);
    }

    /**
     * 获取机构树
     *
     * @param treeBean
     * @return
     */
    @RequestMapping("/treeList")
    public R tree(TreeBean treeBean) {
        logger.info("获取机构树");
        treeBean.setLabel("全部机构信息");
        List<Mechanism> mechanismList = mechanismService.getMechanismList();
        List<Object> list = new ArrayList<>();
        for (Mechanism mechanism : mechanismList) {
            String name = mechanism.getName();
            Integer id = mechanism.getId();
            Map<String, Object> objMap = new HashMap<>();
            objMap.put("label", name);
            objMap.put("id", id);
            list.add(objMap);
        }
        treeBean.setChildren(list);
        List<Object> treeList = new ArrayList<>();
        treeList.add(treeBean);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("treeList", treeList);
        return R.ok(resultMap);
    }
}
